var util = require('../../utils/util');
var wxApi = require('../../utils/wxApi');
var wxRequest = require('../../utils/wxRequest');
import config from '../../utils/config';
var app = getApp();
var that;

//获取应用实例
var app = getApp()
Page({

    data: {
        ydj:{},
    SysSecond:0,
    InterValObj:null,
    intertxt:"",
    showmark:false,
    showmark_self:false,
    markers: [
    ],
    maplat:24.371883,
    maplng:109.404516,
    mapscale:14,
    mapdistance:0,
    //polyline="{{polyline}}"
    polyline: [{
        points: [{
            latitude: 24.371883,
            longitude: 109.404516,
        }, {
            latitude: 24.338430,
            longitude: 109.433170,
        }],
        color:"#FF0000DD",
        width: 2,
        dottedLine: false,
        arrowLine:true,
    }],


    user: {}
    ,itf14:""
    ,pageInex:1
    ,isload:0
    ,tid:0
    ,team:null
    ,ydj:null
    ,group:[]
    ,sgid:0
    ,showgroup:true
    ,userall:0
    ,stepall:0
    ,team_pm:[]
    ,group_index: 0

    ,isLoadMore : false
    ,isEnd : false
    ,isEmpty:false
    ,isdown:false
    ,team_type:0
    ,cjt:0
    ,yjt:0
    ,cjtstr:"距离比赛结束"
    ,wks:true
    ,rens:0
    ,list:[]
    ,rens:0
    ,step:0
    ,pageInex:1
    ,joingroup:false
    ,zpm:0
    ,tpm:0
    ,hasgroup:true
    ,gname:""
    ,gid :0
    ,changegruop:false
    ,tc:0
      ,cur_tab:"1"
      ,css_jr:"img31-2 "
      ,css_lj:"img10-2"
      ,css_fjr:" c_f1 "
      ,css_flj:""

      ,istcVisible: false
      ,turl:""
        ,intertype:""
        ,intertxt2:""
        ,zjbs:0
        ,noload:0
        ,jstypetxt:"生成海报"
        ,jstype:""
        ,jstxt:""
  },
  onShareAppMessage: function () {
      return app.getShare();
  },
  onShow:function(){

    that.data.isload = 0;
    that.setData({
      list: [],
      isLoadMore: false,
      isEnd: false,
      isEmpty: false,
      isdown: true,
      pageInex: 1
    });
    if(that.data.noload==1){
        that.data.noload=0;
    }else{
        that.loadData();
    }

  },
  onReady:function(){
    
    
  },
  onReachBottom : function(e,a){
      //加载更多
      if(!that.data.isEnd && !that.data.isLoadMore && !that.data.isEmpty){
          that.loadList();
      }
  },

  bindPickerChange: function(e) {
    
    this.setData({
      team_index: e.detail.value
    })
  },
  onPullDownRefresh:function(){
    that.setData({
      list : [],
      isLoadMore : false,
      isEnd : false,
      isEmpty:false,
      isdown:true,
      pageInex:1
    });
    that.loadData();
  },
  onLoad: function (options) {
    that = this;
    that.data.tc=0;
    clearTimeout(this.runcheck);
    that.data.isload=1;
    that.data.pageInex=1;
    if( !util.judge.isNull(options.tid) ){
      that.data.tid = parseInt(options.tid);
    }

    if(app.globalData.tid_civ!=null){
        if(app.globalData.tid_civ !=that.data.tid ){
            clearInterval(app.globalData.SetRemainTime);
            app.globalData.SetRemainTime= null;
      
            clearInterval(app.globalData.ksbs);
            app.globalData.ksbs= null;
        }
        app.globalData.tid_civ =that.data.tid;
    }

    if(!util.judge.isNull(options.zjbs) ){
        that.data.zjbs = parseInt(options.zjbs);
        if(that.data.zjbs ==1){
            that.data.noload=1;
            that.btnhb();
        }
    }

  },
  onHide:function(){
      that.data.tc=0;
        clearTimeout(this.runcheck);
  },
  onUnload:function(){
      that.data.tc=0;
      clearTimeout(this.runcheck);

      //记录时间 最后的位置
      //wx.setStorageSync('SetRemainTime', '');
      //clearInterval(that.data.InterValObj);

      //wx.setStorageSync('ksbs', '');
      //clearInterval(that.data.ksbs);

     
      clearInterval(app.globalData.SetRemainTime);
      app.globalData.SetRemainTime= null;

      clearInterval(app.globalData.SetRemainTime_2);
      app.globalData.SetRemainTime_2= null;
      
      clearInterval(app.globalData.ksbs);
      app.globalData.ksbs= null;

      clearInterval(app.globalData.ksbs_2);
      app.globalData.ksbs_2= null;
  },
  runcheck:function(){
      if(that.data.tc==1){
          util.hideLoading();
          util.alert("手机微信运动数据获取超时！");

          var url = config.url_save_noyd;
          var params = {}
          wxRequest.postRequest(url, params);
      }
  },
  SetRemainTime:function(){
       
      if (that.data.SysSecond > 0) {

            that.data.SysSecond = that.data.SysSecond - 1;


            if(that.data.bszt ==1){
                var second = Math.floor(that.data.SysSecond % 60);             // 计算秒      
                var minite = Math.floor((that.data.SysSecond / 60) % 60);      //计算分  
                var hour = Math.floor((that.data.SysSecond / 3600) % 24);      //计算小时  
                var day = Math.floor((that.data.SysSecond / 3600) / 24);        //计算天 
                var t = hour + "小时" + minite + "分" + second + "秒";

                var stt = "";
                if(that.data.ydj.isstart ==0 && that.data.ydj.isend ==0){
                    stt="您未开始竞赛";
                }  else if(that.data.ydj.isstart ==1 && that.data.ydj.isend ==0){
                    stt="您正在竞赛中";
                }
                else if(that.data.ydj.isend ==1){
                    stt="您已完成竞赛";
                }

                that.setData({
                    intertxt:t
                    ,intertxt2:"距离结束:"
                    ,intertype:stt
                });
            }



        } else {//剩余时间小于或等于0的时候，就停止间隔函数  
            if(that.data.bszt ==0){
                that.data.SysSecond =that.data.bszt_1_ss;
                that.data.bszt =1;
                //try {
                //    var value = wx.getStorageSync('ksbs');
                //    if (!value) {
                //        wx.setStorageSync('ksbs', 1);
                //        that.ksbs();
                //        that.data.ksbs = setInterval(that.ksbs, 10000);
                //    }
                //} catch (e) {
                     
                //}

                that.ksbs();
                if(app.globalData.ksbs==null){
                    app.globalData.ksbs= setInterval(that.ksbs, 1000);
                }

            }else{
                that.data.bszt =2;
                that.setData({
                    intertxt:"比赛已结束"
                    ,intertxt2:""
                    ,intertype:"您已完成竞赛"
                });
                //记录时间 最后的位置
                //wx.setStorageSync('SetRemainTime', '');
                //clearInterval(that.data.InterValObj);

                
                clearInterval(app.globalData.SetRemainTime);
                app.globalData.SetRemainTime= null;

                //wx.setStorageSync('ksbs', '');
                //clearInterval(that.data.ksbs);
                
                clearInterval(app.globalData.ksbs);
                app.globalData.ksbs= null;

            }
      }




  },  

  SetRemainTime_2:function(){
      if(that.data.bszt ==0){

          that.setData({
              intertxt:""
              ,intertxt2:""
              ,intertype:"未进入起点"
          });

          that.ksbs_2();
          if(app.globalData.ksbs_2==null){
              app.globalData.ksbs_2= setInterval(that.ksbs_2, 1000);
          }

      }else if(that.data.bszt ==1){
          if (that.data.SysSecond > 0) {
              that.data.SysSecond = that.data.SysSecond - 1;

              if(that.data.bszt ==1){
                  var second = Math.floor(that.data.SysSecond % 60);             // 计算秒      
                  var minite = Math.floor((that.data.SysSecond / 60) % 60);      //计算分  
                  var hour = Math.floor((that.data.SysSecond / 3600) % 24);      //计算小时  
                  var day = Math.floor((that.data.SysSecond / 3600) / 24);        //计算天 
                  var t = hour + "小时" + minite + "分" + second + "秒";

                  var stt = "";
                  
                  that.setData({
                      intertxt:t
                      ,intertxt2:""
                      ,intertype:stt
                  });
              }

              that.ksbs_2();
              if(app.globalData.ksbs_2==null){
                  app.globalData.ksbs_2= setInterval(that.ksbs_2, 1000);
              }

          }else{
              that.data.bszt =2;
              that.setData({
                  intertxt:""
                  ,intertxt2:""
                  ,intertype:"已超时"
              });
              clearInterval(app.globalData.SetRemainTime_2);
              app.globalData.SetRemainTime_2= null;
              clearInterval(app.globalData.ksbs_2);
              app.globalData.ksbs_2= null;
          }
      }
  },
  ksbs_2:function(){
      wx.getLocation({
          type: 'gcj02', //返回可以用于wx.openLocation的经纬度
          success: function(res) {
              var latitude = res.latitude;
              var longitude = res.longitude;
              console.log(latitude);
              console.log(longitude);
              console.log(parseFloat(that.data.team.qdlat));
              console.log(parseFloat(that.data.team.qdlng));
              
              //当前是否已经结束
              var s =0;
                  if(that.data.ydj.isstart ==0 && that.data.ydj.isend ==0){
                      //判断是否进入到 比赛开始点
                      s = that.getDistance(latitude,longitude,parseFloat(that.data.team.qdlat),parseFloat(that.data.team.qdlng));
                      console.log(s);
                      if(s>0 && s <that.data.team.dura ){
                          //记录用户进入比赛
                          var url = config.url_save_yd_js;
                          var  tid = that.data.tid;
                          var params = {tid:tid,dura:s,isbegin:1}
                          wxRequest.postRequest(url, params) 
                          .then(res=>{
                              if(util.judge.isObject(res)){
                                  that.data.ydj.isstart =1;
                                  //手机振动
                                  wx.vibrateLong();
                                  //刷新数据
                                  that.loadList(true);
                              }
                          })
                      }
                  }
                  else if(that.data.ydj.isstart ==1 && that.data.ydj.isend ==0){
                      //判断是否进入到 结束点
                      s = that.getDistance(latitude,longitude,parseFloat(that.data.team.zdlat),parseFloat(that.data.team.zdlng));
                      console.log(s);
                      if(s>0 && s <that.data.team.zddura ){
                          //记录用户进入比赛
                          var url = config.url_save_yd_js;
                          var  tid = that.data.tid;
                          var params = {tid:tid,dura:s,isbegin:0}
                          wxRequest.postRequest(url, params) 
                          .then(res=>{
                              if(util.judge.isObject(res)){

                                  that.data.ydj.isend =1;

                                  //手机振动
                                  wx.vibrateLong();
                                  //刷新数据
                                  that.loadList(true);
                              }
                          })
                      }
                  }
                  else {
                      clearInterval(app.globalData.ksbs_2);
                      app.globalData.ksbs_2= null;
                  }
          }
      })
  }, 

  ksbs:function(){
      wx.getLocation({
          type: 'gcj02', //返回可以用于wx.openLocation的经纬度
          success: function(res) {
              var latitude = res.latitude;
              var longitude = res.longitude;
              console.log(latitude);
              console.log(longitude);
              console.log(parseFloat(that.data.team.qdlat));
              console.log(parseFloat(that.data.team.qdlng));
              
              //当前是否已经结束
              var s =0;
              if(that.data.bszt ==1){

                  if(that.data.ydj.isstart ==0 && that.data.ydj.isend ==0){
                      //判断是否进入到 比赛开始点
                      s = that.getDistance(latitude,longitude,parseFloat(that.data.team.qdlat),parseFloat(that.data.team.qdlng));
                      console.log(s);
                      if(s>0 && s <that.data.team.dura ){
                          //记录用户进入比赛
                          var url = config.url_save_yd_js;
                          var  tid = that.data.tid;
                          var params = {tid:tid,dura:s,isbegin:1}
                          wxRequest.postRequest(url, params) 
                          .then(res=>{
                              if(util.judge.isObject(res)){

                                  that.data.ydj.isstart =1;

                                  //手机振动
                                  wx.vibrateLong();
                                  //刷新数据
                                  that.loadList(true);
                              }
                          })
                      }
                  }
                  else if(that.data.ydj.isstart ==1 && that.data.ydj.isend ==0){
                      //判断是否进入到 结束点
                      s = that.getDistance(latitude,longitude,parseFloat(that.data.team.zdlat),parseFloat(that.data.team.zdlng));
                      console.log(s);
                      if(s>0 && s <that.data.team.zddura ){
                          //记录用户进入比赛
                          var url = config.url_save_yd_js;
                          var  tid = that.data.tid;
                          var params = {tid:tid,dura:s,isbegin:0}
                          wxRequest.postRequest(url, params) 
                          .then(res=>{
                              if(util.judge.isObject(res)){

                                  that.data.ydj.isend =1;

                                  //手机振动
                                  wx.vibrateLong();
                                  //刷新数据
                                  that.loadList(true);
                              }
                          })
                      }
                  }
                  else {
                      //wx.setStorageSync('ksbs', '');
                      
                      clearInterval(app.globalData.ksbs);
                      app.globalData.ksbs= null;
                  }
              }
          }
      })
  }, 
  jljs:function(dura,isbegin){
      var url = config.url_save_yd_js;
      var  tid = that.data.tid;
      var params = {tid:tid,dura:dura,isbegin:isbegin}
      wxRequest.postRequest(url, params) 
      .then(res=>{
          if(res !=2 && util.judge.isObject(res)){
              that.setData({
                  pm:res.pm
                 ,quality:res.quality
                  ,measure:res.measure
                  ,affect:res.affect
                  ,addr:res.city
                  ,hidpm:false
              });
          }
      })
  }, 


  loadData: function () {
    util.showLoading();
    app.getUserInfo()
    .then(res => {
        console.log(res);
      that.setData({
        user:res
      })

      that.data.tc=1;

      clearTimeout(this.runcheck);
      setTimeout(this.runcheck,5000);
      return wxApi.wxGetWeRunData()();
    })
    .then(res => {
      that.data.tc=0;

      console.log(res);
      if(that.data.tid == 0){
        throw util.stop();
      }

      var url = config.url_user_save_run;
      var params = {
       ed: res.encryptedData,
       key: util.getkey(),
       iv:util.getiv()
      }
      return wxRequest.postRequest(url, params);
    })
    .then(res => {
      console.log(res);
      if(util.judge.isObject(res) && res.hasOwnProperty("id") && res.id > 0 ){
        
      }else{
          throw util.stop({message:"手机微运动数据获取失败！"});
      }

      //这里要取的是团的更新步数 而不是当前的步数
        var step = parseInt(res.step);
        that.setData({
            step:step
        }); 

      var id= that.data.tid;
      var url = config.url_team_user_update;
      var params = {
        id:id
      }
      return wxRequest.postRequest(url, params);
    })
    .then(res=>{
        console.log(res);
      if(res ==22){
        wx.redirectTo({
          url: '../team/view?tid='+ that.data.tid
        });
        throw util.stop();
      }


      var url = config.url_team_get;
      var params = {
        id:that.data.tid,
        code:'pm'
      }
      return wxRequest.postRequest(url, params)
    })
    .then(res => {
        console.log(res);

        if(util.judge.isObject(res)){

            var team =res.team;
            var turl="https://app.dkcake.com/images/35.jpg";
            if(util.judge.isString(team.url) && team.url != ""){
                turl="https://app.dkcake.com"+team.url;
            }
            that.setData({turl:turl});


          that.data._sdesc="D几名邀请您参加\""+team.name+ "\"比赛！" ;

          wx.setNavigationBarTitle( { title: team.name });
          if(team.jstype=="10000"){
              that.setData({
                  jstypetxt:"我的状态"
              });
          }
          that.setData({
              team_type:team.type,
              jstype:team.jstype,
              jstxt:res.jstxt
          });

          var bd = util.str2date(team.bdate);
          var ed = util.str2date(team.edate);
          
          var type = team.type;

          if(type ==1 || type == 2){
              that.setData({
                  ydj:res.ydj
              });

              that.data.bszt_1_ss=0;
              that.data.bszt =0;

              if(type ==1){
                  var tgn = res.tgn;
                  var tgb = res.tgb;
                  var tge = res.tge;

                  var s1 = that.GetDateDiff(tgn,tgb,"second");
                  var s2 = that.GetDateDiff(tgb,tgn,"second");
                  var s3 = that.GetDateDiff(tgn,tge,"second");
                  that.data.bszt_1_ss=s3;
                  var s4 = that.GetDateDiff(tge,tgn,"second");
                  var ss=0;

                  if(s1 >0){
                      //比赛未开始
                      that.data.bszt =0;
                      that.setData({
                          intertxt:"比赛开始时间 "+tgb
                          ,intertype:""
                      });//,team_pm:res.pm
                      ss = s1;
                  }else if(s2 >= 0 && s3 >= 0 ){
                      //比赛进行中
                      that.data.bszt =1;
                      that.data.jrbs=0;
                      ss = s3;
                  }else{
                      //比赛已结束
                      that.data.bszt =2;
                      that.setData({
                          intertxt:"比赛已结束"
                          ,intertype:""
                      });
                  }

                  if(that.data.bszt < 2){
                      that.data.SysSecond = parseInt(ss); //这里获取倒计时的起始时间  
                      if(app.globalData.SetRemainTime==null){
                          app.globalData.SetRemainTime= setInterval(that.SetRemainTime, 1000);
                      }
                  }
                  if(that.data.bszt ==1){
                      if(app.globalData.ksbs==null){
                          app.globalData.ksbs= setInterval(that.ksbs, 1000);
                      }
                  }

                   
              }else{
                  var isstart = res.isstart;
                  var isend = res.isend;

                  if(isstart == 0){
                      //开始循环记录是否进入起点
                      that.data.bszt =0;
                  }else if (isstart == 1 && isend == 0 ){
                      //开始倒计时 几个小时
                      that.data.bszt =1;

                      var tgn = res.tgn;
                      var tgb = res.tgb;
                      var tge = res.tge;

                      var ss = that.GetDateDiff(tgn,tge,"second");
                      that.data.SysSecond = parseInt(ss); //这里获取倒计时的起始时间  

                  }else{
                      //完结当天的活动了
                      that.data.bszt =2;
                      that.setData({
                          intertxt:""
                          ,intertxt2:""
                          ,intertype:"已到达"
                      });
                  }

                  if(that.data.bszt ==0){
                      if(app.globalData.SetRemainTime_2==null){
                          app.globalData.SetRemainTime_2= setInterval(that.SetRemainTime_2, 1000);
                      }
                  }

                  if(that.data.bszt ==1){
                      if(app.globalData.SetRemainTime_2==null){
                          app.globalData.SetRemainTime_2= setInterval(that.SetRemainTime_2, 1000);
                      }

                      if(app.globalData.ksbs_2==null){
                          app.globalData.ksbs_2= setInterval(that.ksbs_2, 1000);
                      }
                  }
              }

              var jspz  = res.jspz;
              that.setData({
                  showmark:true,
                  mapscale:jspz.mapscale,
                  mapdistance:jspz.distance,
                  markers: [
                     {
                         iconPath: "../../assets/images/begin.png",
                         id: 0,
                         latitude: team.qdlat,
                         longitude: team.qdlng,
                         width: jspz.wh,
                         height: jspz.wh,
                         callout:{
                             content:team.qdname,
                             color: '#FF0000',
                             fontSize: 12,
                             borderRadius: 5,
                             padding:2,
                             display: 'ALWAYS'
                         }
                     },
                     {
                         iconPath: "../../assets/images/end.png",
                         id: 1,
                         latitude: team.zdlat,
                         longitude: team.zdlng,
                         width: jspz.wh,
                         height: jspz.wh,
                         callout:{
                             content:team.zdname,
                             color: '#FF0000',
                             fontSize: 12,
                             borderRadius: 5,
                             padding:2,
                             display: 'ALWAYS'
                         }
                     }
                  ],
                  maplat: team.qdlat,
                  maplng: team.qdlng
              });

          }else{
          
          }

            //判断比赛类型
            //速度赛
            //填入起始坐标
            //展示地图
            //读取后台配置信息



          res.team.bdate = util.date2str(team.bdate,"MM.dd");
          res.team.edate = util.date2str(team.edate,"MM.dd");

          var d1 = new Date();    //结束时间  
          if(team.isclose ==1){
                that.setData({cjtstr:"比赛已关闭",wks:false});
          }else{
                if(d1.getTime() > ed.getTime()){
                    that.setData({cjtstr:"比赛已结束",wks:false});
                }
                else if(bd.getTime() > d1.getTime()){
                    that.setData({cjtstr:"比赛未开始",wks:false});
                }
                else{
                    //进行中
                    
                    var date3 = ed.getTime() - d1.getTime();   //时间差的毫秒数 
                    var days=parseFloat(date3/(24*3600*1000));  
                    if(days < 1){
                        that.setData({cjtstr:"比赛今天结束",wks:false});
                    }else{
                        days=that.modFoat(days);
                        that.setData({ cjtstr: "距离比赛结束", wks: true });
                    }

                    if(type ==1){

                    }else{
                    }
                    
               }
          }

          date3 = ed.getTime() - bd.getTime();   //时间差的毫秒数        
          var yjt = parseInt(parseFloat(date3/(24*3600*1000) + 0.00001))+1; 
          that.setData({cjt:days,yjt:yjt});

          that.setData({
            team:res.team,
            stepall:res.stepall,
            userall:res.userall
          });//,team_pm:res.pm


          if(type ==1){
              //比赛还在进行  比赛结束了 就不管了?->问 比赛时间结束了,如果用户还在进行中怎么办? 列表中的数据显示未完成.?还是显示什么内容?环节怎么处理?,
              //所以需要再js上记录当前的系统时间? 用一个 轮训来 记录当前的js 时间.


              //判断用户当前状态 
              //完成的什么都不做
              //未开始的 //判断用户是否进入到比赛地点

              //进行中的 开始判断用户是否进入到比赛终点

              //上述是两个方法,因为用户在进入比赛地点后,会刷新下面的数据列表,那么就开始判断是否进入比赛终点了.

              //

          }
        }
    })
    .then(res=>{ 
        console.log(res);
      var url = config.url_team_data_get;
      var params = {
        id:that.data.tid
      }
      return wxRequest.postRequest(url, params)
    })
    .then(res => {
        console.log(res);
      if(util.judge.isObject(res)){
        that.setData({
          ydata:res
          ,gid:res.gid
        });
      }
      var url = config.url_team_group;
      var params = {
        id:that.data.tid
      }
      return wxRequest.postRequest(url, params)
    })
    .then(res => {
        console.log(res);
        if(util.judge.isArray(res)){
          that.setData({
            group:res
          });

          if(that.data.gid ==0 && res.length >0){
            that.setData({
              joingroup:true
            });
            throw util.stop();
          }

          if(res.length >0){
              var gname ="";
              for(var i in res){
                if(res[i].id == that.data.gid){
                  gname = res[i].name;
                  break;
                }
              }

              that.setData({
                hasgroup:true,
                gname :gname
              });
          }else{

            that.setData({
                hasgroup:false
              });
          }
        }else{
          throw util.stop();
        }

        that.setData({
          joingroup:false
        });
        that.loadList();

        //判断是否计算排名
        //if(!that.data.hasgroup){
        //  throw util.stop();
        //}

        var myDate = new Date();
        var date = myDate.getFullYear()+"-"+myDate.getMonth()+"-"+myDate.getDate();
        var guid =  app.globalData.user.uid+"-"+that.data.tid;
        var tc = wx.getStorageSync(guid);
        if(tc == date){
            //throw util.stop();
        }
        else{
            that.data.guid = guid;
            that.data.guid_date = date;
        }

        var url = config.url_team_pm;
        if(that.data.team.type ==1){
            url = config.url_team_pm_js;
        }
        var params = {
          id:that.data.tid
          ,gid:that.data.gid
        }
        return wxRequest.postRequest(url, params)
    })
    .then(res => {
        console.log(res);
      if(util.judge.isObject(res)){
        that.setData({
            tpm:res.tpm,
            zpm:res.zpm
        });

        if(that.data.team.type ==2){
            this.setData({
                itf14:"f14",
                showmark_self:true
            });
        }

        var myDate = new Date();
        var date = myDate.getFullYear()+"-"+myDate.getMonth()+"-"+myDate.getDate();
        var guid =  app.globalData.user.uid+"-"+that.data.tid;
        var tc = wx.getStorageSync(guid);
        if(tc == date){
            that.setData({
               showmark_self:that.data.showmark
            });
           throw util.stop();
        }

       if(that.data.team.type ==0 || that.data.team.type ==1){

            //用户每天就弹出一次 这里注意一下
            var animation = wx.createAnimation({
                timingFunction: "linear",
            })
            animation.scale(0.1, 0.1).step();
            this.setData({
                coverAlphaAnim: animation.export(),
                opop: 1,
                istcVisible: true
            });

            animation.scale(1, 1).step({ duration: 250 });
            this.setData({
                coverAlphaAnim: animation.export()
            });
          
       }
       
       
      }
    })
    .catch(res => {
        console.log(res);
        util.alert_catch(res.message);
    })
    .finally(function (res) {
        util.hideLoading()
        wx.stopPullDownRefresh();
    });
    
  },
 
  loadList: function(reload){
      if(reload ==true){
          that.setData({
              list : [],
              isLoadMore : false,
              isEnd : false,
              isEmpty:false,
              isdown:true,
              pageInex:1
          });
      }
      
    that.setData({
      isLoadMore : true
    });

    var id= that.data.tid;
    var gid =that.data.gid;
    var url = config.url_team_user;
    if(that.data.hasgroup){
        url = config.url_group_user;
    }

    if(that.data.team.type==1 || that.data.team.type==2  ){
        url = config.url_team_user_js;
    }
 

    var params = {
      pageIndex: that.data.pageInex,
      pageSize: app.globalData.defaultPageSize,
      id:id,
      gid : gid,
      tab: that.data.cur_tab
    }

    wxRequest.postRequest(url, params)
    .then(res=>{
      
      if(that.data.pageInex==1 && res.length==0){
          that.setData({
            isLoadMore : false,
            isEnd : false,
            isEmpty :true,
          });
      }
      
      if(res.length > 0){
          if(that.data.isdown==true){
              that.data.list =[];
              that.data.isdown=false;
          }

        var data = that.data.list.concat(res);
        that.setData({
            list : data,
            isLoadMore : false
        });
        that.data.pageInex++;
      }
      
      if(res.length < app.globalData.defaultPageSize || res.length == 0 ){
        that.setData({
          isLoadMore : false,
          isEnd : true
        });
      }

    })
    .catch(res => {
      util.alert(res.message);
    })
    .finally(function (res) {
      util.hideLoading();
    });
  },

  btnJoin:function(){
    //判断团是否有组,有组就显示,没有就直接加入吧
    if(that.data.group.length>0){
      if(that.data.sgid ==0){
        util.alert("请选择要加入组");
        return;
      }
    }

    util.showLoading();

    var url = config.url_team_user_in;
    var params = {
      id:that.data.tid,
      gid :that.data.sgid
    }

    wxRequest.postRequest(url, params)
    .then(res => {

      if(res==1){
        util.alert("加入成功");

        wx.redirectTo({
          url: '../team/main?tid='+ that.data.tid
        });
      
      }else if(res==22){
        util.alert("请勿重复加入团队");
      }

    })
    .catch(res => {
        util.alert(res.message);
    })
    .finally(function (res) {
        util.hideLoading();
    });


  },
  radioChange: function(e) {
    console.log(e.detail.value);
    that.data.sgid = e.detail.value;
  },
  addGroup:function(e){
    var t = 0;
    var gid = e.currentTarget.dataset.id;
    
    util.showLoading();

    var url = config.url_group_user_in;

    var params = {
      tid:that.data.tid,
      id :gid,
      gid:that.data.gid
    }
 
    if(that.data.changegruop==true){
        url = config.url_group_user_change;
    }

    wxRequest.postRequest(url, params)
    .then(res => {
        if(res==1){
            var str = "加入成功";
            util.alert(str,function(){
                that.onPullDownRefresh();
            });
        }
    })
    .catch(res => {
        util.alert(res.message);
    })
    .finally(function (res) {
        util.hideLoading();
    });
  }
    , modFoat:function(v) {
        var _max = parseInt(v) + 1;
        if( _max - v < 1 ) {
            return _max;
        }
        return v;
    } 

    ,btnShowTeam:function(){
        wx.navigateTo({
            url: '../team/mainall?tid='+ that.data.tid
        });
    }
    ,btnhb:function(){
        util.showLoading();
    
        var url = config.url_hb_home;
        var params = {
            tid:that.data.tid,
            zjbs:that.data.zjbs
        }
        wxRequest.postRequest(url, params)
        .then(res => {
            res = app.globalData.url_local+res;
            var arr=[];
            arr.push(res);
            wx.previewImage({
                urls: arr
            })
        })
        .catch(res => {
            util.alert(res.message);
        })
        .finally(function (res) {
            util.hideLoading();
        });
    }
    ,btnhz:function(){
        that.setData({joingroup:true,changegruop:true});
    }
    ,btnqxhz:function(){
        that.setData({joingroup:false,changegruop:false});
    }
    ,btnts:function()
    {
        util.confirm("确定要退出竞赛吗?",function(res){
            if (res.confirm) {

                util.showLoading();
    
                var url = config.url_team_user_out;
                var params = {
                    tid:that.data.tid
                    ,ot:0 //退出模式
                    ,id:app.globalData.user.uid//退出人
                }
                wxRequest.postRequest(url, params)
                .then(res => {
                    if(res==1){
                        wx.switchTab({
                            url: '../team/index'
                        });
                        throw util.stop();
                    }else{
                        util.alert("操作失败");
                    }
                
                })
                .catch(res => {
                    util.alert_catch(res.message);
                })
                .finally(function (res) {
                    util.hideLoading();
                });
            }
        });
    }
    ,btnTab:function(e){
        util.showLoading();

        var tab = e.currentTarget.dataset.tab;
        if(tab == 0){
            that.setData({
                    cur_tab:"0"
                  ,css_jr:"img31 "
                  ,css_lj:"img10"
                  ,css_fjr:" "
                  ,css_flj:" c_f1 "
            })
        }else{
            that.setData({
                cur_tab:"1"
                ,css_jr:"img31-2 "
                ,css_lj:"img10-2"
                ,css_fjr:" c_f1 "
                ,css_flj:""
            })
        }
        that.loadList(true);
    },
    closeTc:function(e){
        var url = config.url_user_instm;
        var params = {
            fid:e.detail.formId
        }

        wxRequest.postRequest(url, params)
        .then(res=>{
            if(res==1){
                wx.setStorageSync(that.data.guid,that.data.guid_date);
            }
        })

        this.setData({
            showmark_self:that.data.showmark,
            istcVisible: false
        });
    }
    ,markertap:function(e){
        //util.alert(e.markerId); //20	
        if(e.markerId==0){
            wx.openLocation({
                latitude: parseFloat( that.data.team.qdlat),
                longitude: parseFloat(that.data.team.qdlng),
                scale: 28
            })
        }else{
            wx.openLocation({
                latitude: parseFloat(that.data.team.zdlat),
                longitude: parseFloat(that.data.team.zdlng),
                scale: 28
            })
        }
    }
    ,GetDateDiff:function(startTime, endTime, diffType) {
    //将xxxx-xx-xx的时间格式，转换为 xxxx/xx/xx的格式 
    startTime = startTime.replace(/-/g, "/");
    endTime = endTime.replace(/-/g, "/");
    //将计算间隔类性字符转换为小写 
    diffType = diffType.toLowerCase();
    var sTime = new Date(startTime); //开始时间 
    var eTime = new Date(endTime); //结束时间 
    //作为除数的数字 
    var divNum = 1;
    switch (diffType) {
        case "second":
            divNum = 1000;
            break;
        case "minute":
            divNum = 1000 * 60;
            break;
        case "hour":
            divNum = 1000 * 3600;
            break;
        case "day":
            divNum = 1000 * 3600 * 24;
            break;
        default:
            break;
    }
    return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(divNum)); //17jquery.com 
    }
    /**
     * 获取两个经纬度之间的距离
     * @param lat1 第一点的纬度
     * @param lng1 第一点的经度
     * @param lat2 第二点的纬度
     * @param lng2 第二点的经度
     * @returns {Number}
     */
    ,getDistance:function(lat1,lng1,lat2,lng2){
        var f = that.getRad((lat1 + lat2)/2);     
        var g = that.getRad((lat1 - lat2)/2);     
        var l = that.getRad((lng1 - lng2)/2);     
        var sg = Math.sin(g);     
        var sl = Math.sin(l);     
        var sf = Math.sin(f);     
        var s,c,w,r,d,h1,h2;     
        var a = 6378137.0;//The Radius of eath in meter.   
        var fl = 1/298.257;     
        sg = sg*sg;     
        sl = sl*sl;     
        sf = sf*sf;     
        s = sg*(1-sl) + (1-sf)*sl;     
        c = (1-sg)*(1-sl) + sf*sl;     
        w = Math.atan(Math.sqrt(s/c));     
        r = Math.sqrt(s*c)/w;     
        d = 2*w*a;     
        h1 = (3*r -1)/2/c;     
        h2 = (3*r +1)/2/s;     
        s = d*(1 + fl*(h1*sf*(1-sg) - h2*(1-sf)*sg));   
        //s = s/1000;
        s = s.toFixed(0);//指定小数点后的位数。   
        return s; 
    }
    ,getRad:function(d){   
        var PI = Math.PI;    
        return d*PI/180.0;    
    }
    ,btnhome:function(){
        wx.switchTab({
            url: '../index/index'
        });
    }
})